package com.ryder.petmatediarybackend.module.shop.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.ryder.petmatediarybackend.common.response.Result;
import com.ryder.petmatediarybackend.module.shop.dto.CreateQualificationDTO;
import com.ryder.petmatediarybackend.module.shop.service.ShopQualificationService;
import com.ryder.petmatediarybackend.module.shop.vo.QualificationVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述：店铺资质管理 Controller
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0
 * 更新日期: 2025-10-31
 */
@Slf4j
@Tag(name = "店铺资质管理", description = "店铺资质相关接口（需要登录 + 店铺经营者角色）")
@RestController
@RequestMapping("/shop/qualification")
@SecurityRequirement(name = "Authorization")
@RequiredArgsConstructor
public class ShopQualificationController {

    private final ShopQualificationService qualificationService;

    @Operation(summary = "提交店铺资质", description = "提交店铺资质证明，需要 shop:qualification:submit 权限")
    @SaCheckPermission("shop:qualification:submit")
    @PostMapping("/submit")
    public Result<QualificationVO> submitQualification(@Valid @RequestBody CreateQualificationDTO dto) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[资质管理] 用户ID={} 执行提交店铺资质, 方法=submitQualification, 入参={}", userId, dto);
        QualificationVO vo = qualificationService.createQualification(userId, dto);
        log.info("[资质管理] 用户ID={} 提交店铺资质成功, 资质ID={}, 资质名称={}", userId, vo.getId(), vo.getQualificationName());
        return Result.ok(vo);
    }

    @Operation(summary = "获取我的资质列表", description = "获取当前店铺的所有资质，需要 shop:qualification:view 权限")
    @SaCheckPermission("shop:qualification:view")
    @GetMapping("/list")
    public Result<List<QualificationVO>> getMyQualifications() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[资质管理] 用户ID={} 执行获取资质列表, 方法=getMyQualifications", userId);
        List<QualificationVO> list = qualificationService.getMyQualifications(userId);
        log.info("[资质管理] 用户ID={} 获取资质列表成功, 数量={}", userId, list.size());
        return Result.ok(list);
    }

    @Operation(summary = "删除资质", description = "删除指定资质（逻辑删除）")
    @DeleteMapping("/delete/{qualificationId}")
    public Result<Void> deleteQualification(@PathVariable Long qualificationId) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[资质管理] 用户ID={} 执行删除资质, 方法=deleteQualification, 资质ID={}", userId, qualificationId);
        qualificationService.deleteQualification(userId, qualificationId);
        log.info("[资质管理] 用户ID={} 删除资质成功, 资质ID={}", userId, qualificationId);
        return Result.ok();
    }

    @Operation(summary = "申请疫苗资质", description = "申请疫苗权限（需要先通过动物诊疗许可证审核）")
    @PostMapping("/apply-vaccine")
    public Result<Void> applyVaccinePermission() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[资质管理] 用户ID={} 执行申请疫苗资质, 方法=applyVaccinePermission", userId);
        qualificationService.applyVaccinePermission(userId);
        log.info("[资质管理] 用户ID={} 申请疫苗资质成功", userId);
        return Result.ok();
    }
}

